﻿#ifndef WGL_H
#define WGL_H
extern "C"{
#include <GL/glew.h>
#include <GL/wglew.h>
}
#include <qt_windows.h>
#include <QLibrary>
#include <QDebug>


typedef HGLRC (WINAPI * PWGLCREATECONTEXTATTRIBSARBPROC)(HDC,HGLRC,const int*);
typedef BOOL (WINAPI * PWGLGETPIXELFORMATATTRIBIVARBPROC)(HDC,int,int,UINT,const int*,int*);
typedef PROC (WINAPI * P_wglGetProcAddress)(LPCSTR);
typedef const char* (WINAPI * PWGLGETEXTENSIONSSTRINGEXTPROC)(void);
typedef const char* (WINAPI * PWGLGETEXTENSIONSSTRINGARBPROC)(HDC);

struct WGL{
    bool EXT_swap_control;
    bool EXT_colorspace;
    bool ARB_multisample;
    bool ARB_framebuffer_sRGB;
    bool EXT_framebuffer_sRGB;
    bool ARB_pixel_format;
    bool ARB_create_context;
    bool ARB_create_context_profile;
    bool EXT_create_context_es2_profile;
    bool ARB_create_context_robustness;
    bool ARB_create_context_no_error;
    bool ARB_context_flush_control;
    PWGLCREATECONTEXTATTRIBSARBPROC CreateContextAttribsARB;
    PWGLGETPIXELFORMATATTRIBIVARBPROC GetPixelFormatAttribivARB;
    P_wglGetProcAddress WglGetProcAddress;
    PWGLGETEXTENSIONSSTRINGEXTPROC    GetExtensionsStringEXT;
    PWGLGETEXTENSIONSSTRINGARBPROC    GetExtensionsStringARB;
};

struct FBconfig
{
    int         redBits;
    int         greenBits;
    int         blueBits;
    int         alphaBits;
    int         depthBits;
    int         stencilBits;
    int         accumRedBits;
    int         accumGreenBits;
    int         accumBlueBits;
    int         accumAlphaBits;
    int         auxBuffers;
    bool        stereo;
    int         samples;
    bool        sRGB;
    bool        doublebuffer;
    uintptr_t   handle;
};

struct ctxconfig
{
    int           client = 0x00030001;
    int           source = 0x00036001;
    int           major = 1;
    int           minor = 0;
    bool          forward;
    bool          debug;
    bool          noerror;
    int           profile;
    int           robustness;
    int           release;
    struct {
        bool  offline;
    } nsgl;
};

static int getPixelFormatAttrib(HDC dc, int pixelFormat, int attrib);
static const FBconfig* ChooseFBConfig(const FBconfig* desired,
                                         const FBconfig* alternatives,
                                         unsigned int count);
static int choosePixelFormat(HDC dc, HDC helperDC);
bool initWGL(HDC dc, HDC helperDC);
static bool extensionSupportedWGL(const char* extension);
#endif // WGL_H
